package com.zim.ta_service.base.model.resp;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zim.ta_service.base.model.req.PageReq;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

/**
 * author: alabimofa <br>
 * date: 2021/10/22 17:43 <br>
 */
@ApiModel("分页查询结果")
public class PageResp<T> implements IPage<T> {

	/**
	 * 查询数据列表
	 */
	@ApiModelProperty("当前页数据")
	protected List<T> records = Collections.emptyList();
	/**
	 * 总数
	 */
	@ApiModelProperty("数据总数")
	protected long total = 0;
	/**
	 * 每页显示条数，默认 10
	 */
	@JsonIgnore
	protected long size = 10;
	/**
	 * 当前页
	 */
	@JsonIgnore
	protected long current = 1;
	/**
	 * 排序字段信息
	 */
	@Setter
	@JsonIgnore
	protected List<OrderItem> orders = new ArrayList<>();
	/**
	 * 自动优化 COUNT SQL
	 */
	@JsonIgnore
	protected boolean optimizeCountSql = true;
	/**
	 * 是否进行 count 查询
	 */
	@JsonIgnore
	protected boolean searchCount = true;
	/**
	 * countId
	 */
	@Setter
	@JsonIgnore
	protected String countId;
	/**
	 * countId
	 */
	@Setter
	@JsonIgnore
	protected Long maxLimit;

	public PageResp() {}

	/**
	 * 分页构造函数
	 * @param current 当前页
	 * @param size    每页显示条数
	 */
	public PageResp(long current, long size) {
		this(current, size, 0);
	}

	public PageResp(long current, long size, long total) {
		this(current, size, total, true);
	}

	public PageResp(long current, long size, boolean searchCount) {
		this(current, size, 0, searchCount);
	}

	public PageResp(long current, long size, long total, boolean searchCount) {
		if (current > 1) {
			this.current = current;
		}
		this.size = size;
		this.total = total;
		this.searchCount = searchCount;
	}

	/**
	 * 是否存在上一页
	 * @return true / false
	 */
	public boolean hasPrevious() {
		return this.current > 1;
	}

	/**
	 * 是否存在下一页
	 * @return true / false
	 */
	public boolean hasNext() {
		return this.current < this.getPages();
	}

	@Override
	public List<T> getRecords() {
		return this.records;
	}

	@Override
	public PageResp<T> setRecords(List<T> records) {
		this.records = records;
		return this;
	}

	@Override
	public long getTotal() {
		return this.total;
	}

	@Override
	public PageResp<T> setTotal(long total) {
		this.total = total;
		return this;
	}

	@Override
	public long getSize() {
		return this.size;
	}

	@Override
	public PageResp<T> setSize(long size) {
		this.size = size;
		return this;
	}

	@Override
	public long getCurrent() {
		return this.current;
	}

	@Override
	public PageResp<T> setCurrent(long current) {
		this.current = current;
		return this;
	}

	@Override
	public String countId() {
		return this.countId;
	}

	@Override
	public Long maxLimit() {
		return this.maxLimit;
	}

	/**
	 * 查找 order 中正序排序的字段数组
	 * @param filter 过滤器
	 * @return 返回正序排列的字段数组
	 */
	private String[] mapOrderToArray(Predicate<OrderItem> filter) {
		List<String> columns = new ArrayList<>(orders.size());
		orders.forEach(i -> {
			if (filter.test(i)) {
				columns.add(i.getColumn());
			}
		});
		return columns.toArray(new String[0]);
	}

	/**
	 * 移除符合条件的条件
	 * @param filter 条件判断
	 */
	private void removeOrder(Predicate<OrderItem> filter) {
		for (int i = orders.size() - 1; i >= 0; i--) {
			if (filter.test(orders.get(i))) {
				orders.remove(i);
			}
		}
	}

	/**
	 * 添加新的排序条件，构造条件可以使用工厂
	 * @param items 条件
	 * @return 返回分页参数本身
	 */
	public PageResp<T> addOrder(OrderItem... items) {
		orders.addAll(Arrays.asList(items));
		return this;
	}

	/**
	 * 添加新的排序条件，构造条件可以使用工厂
	 * @param items 条件
	 * @return 返回分页参数本身
	 */
	public PageResp<T> addOrder(List<OrderItem> items) {
		orders.addAll(items);
		return this;
	}

	@Override
	public List<OrderItem> orders() {
		return this.orders;
	}

	@Override
	public boolean optimizeCountSql() {
		return optimizeCountSql;
	}

	@Override
	public boolean searchCount() {
		if (total < 0) {
			return false;
		}
		return searchCount;
	}

	public PageResp<T> setSearchCount(boolean searchCount) {
		this.searchCount = searchCount;
		return this;
	}

	public PageResp<T> setOptimizeCountSql(boolean optimizeCountSql) {
		this.optimizeCountSql = optimizeCountSql;
		return this;
	}

	@Override
	@ApiModelProperty("总页数")
	public long getPages() {
		return IPage.super.getPages();
	}

	/* --------------- 以下为静态构造方式 --------------- */
	public static <T> PageResp<T> of(long current, long size) {
		return of(current, size, 0);
	}

	public static <T> PageResp<T> of(long current, long size, long total) {
		return of(current, size, total, true);
	}

	public static <T> PageResp<T> of(long current, long size, boolean searchCount) {
		return of(current, size, 0, searchCount);
	}
	public static <T> PageResp<T> of(long current, long size, long total, boolean searchCount) {
		return new PageResp<T>(current, size, total, searchCount);
	}
	public static <T> PageResp<T> of(PageReq req) {
		return of(req.getCurrent(), req.getSize());
	}

	public static <T> PageResp<T> change(PageResp resp, List<T> list) {
		PageResp<T> pageResp = new PageResp<T>(resp.getCurrent(), resp.getSize(), resp.getTotal(), true);
		pageResp.setRecords(list);
		return pageResp;
	}

	public static <T> PageResp<T> change(Page resp, List<T> list) {
		PageResp<T> pageResp = new PageResp<T>(resp.getCurrent(), resp.getSize(), resp.getTotal(), true);
		pageResp.setRecords(list);
		return pageResp;
	}
}
